package com.spzx.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.domain.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.common.rabbit.service.RabbitService;
import com.spzx.order.api.RemoteOrderInfoService;
import com.spzx.order.api.domain.OrderInfo;
import com.spzx.order.api.domain.OrderItem;
import com.spzx.payment.domain.PaymentInfo;
import com.spzx.payment.mapper.PaymentInfoMapper;
import com.spzx.payment.service.IPaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 付款信息Service业务层处理
 *
 * @author atguigu
 * @date 2024-10-17
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements IPaymentInfoService {
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private RemoteOrderInfoService remoteOrderInfoService;

    @Autowired
    private RabbitService rabbitService;

    @Override
    public PaymentInfo savePaymentInfo(String orderNo) {
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo,orderNo));
        if(paymentInfo == null){
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(SecurityContextHolder.getUserId());
            paymentInfo.setOrderNo(orderNo);
            //paymentInfo.setPayType(2); //最好支付后再更新类型。    1微信     2支付宝
            R<OrderInfo> orderInfoResult = remoteOrderInfoService.getByOrderNo(orderNo, SecurityConstants.INNER);
            OrderInfo orderInfo = orderInfoResult.getData();
            //paymentInfo.setAmount(orderInfo.getTotalAmount());
            paymentInfo.setAmount(new BigDecimal(0.01));       // 测试只付1分钱。
            String content = "";
            List<OrderItem> orderItemList = orderInfo.getOrderItemList();
            for (OrderItem orderItem : orderItemList) {
                content += orderItem.getSkuName()+";";
            }
            paymentInfo.setContent(content); //商品名称信息
            paymentInfo.setPaymentStatus("0"); //支付状态：0-未支付 1-已支付 -1-关闭
            paymentInfoMapper.insert(paymentInfo);
        }

        return paymentInfo;
    }

    @Override
    public void updatePaymentStatus(Map<String, String> paramMap, int payType) {
        String orderNo = (String)paramMap.get("out_trade_no");
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo,orderNo));
        if("1".equals(paymentInfo.getPaymentStatus())){ //去重处理，已经更新成 已支付状态，就不用重复更新了。
            return;
        }

        paymentInfo.setPayType(2); //付款方式：1-微信 2-支付宝
        paymentInfo.setTradeNo(paramMap.get("trade_no")); //交易编号（微信或支付）
        paymentInfo.setPaymentStatus("1"); //支付状态：0-未支付 1-已支付 -1-关闭
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(paramMap));
        paymentInfoMapper.updateById(paymentInfo);

        //基于MQ通知订单系统，修改订单状态
        rabbitService.sendMessage(MqConst.EXCHANGE_PAYMENT_PAY, MqConst.ROUTING_PAYMENT_PAY, paymentInfo.getOrderNo());
    }
}